package com.dfrz.bookstore.service.impl;

import com.dfrz.bookstore.dao.impl.UserDaoImpl;
import com.dfrz.bookstore.entry.Admin;
import com.dfrz.bookstore.entry.User;
import com.dfrz.bookstore.service.UserService;
import com.dfrz.bookstore.util.CommenUtil;
import com.dfrz.bookstore.util.SendEmaiUtil;
import com.dfrz.bookstore.view.Client;
import com.dfrz.bookstore.view.Login;

import javax.mail.MessagingException;
import javax.swing.*;
import java.util.List;
import java.util.Random;

/**
 * 类功能描述:
 *
 * @Author keyan
 * @Date 2021/5/22 11:24
 */
public class UserServiceImpl implements UserService {
    UserDaoImpl userDao = new UserDaoImpl();
    @Override
    public User loginBypassWord(User user) {
        User userLogin = userDao.login(user);
        if(userLogin!=null){
            return userLogin;
        }else {
            JOptionPane.showMessageDialog(null,"账号或密码错误");
            return null;
        }
    }

    @Override
    public User loginByEmail(String email, String varCode_input, String varCode) {

        User user = new User();
        user.setEmail(email);
        /*先查找是否存在邮箱是否存在*/
        User userLogin = userDao.loginByEmail(user);
        if (userLogin!=null){
            /*判断验证码是否正确*/
            if (varCode.equals(varCode_input)){
                JOptionPane.showMessageDialog(null,"用户:"+userLogin.getName()+"邮箱登录成功");
                //在这里创建用户窗体
                return userLogin;
            }else {
                JOptionPane.showMessageDialog(null,"验证码错误");
                return null;
            }
        }else {
            JOptionPane.showMessageDialog(null,"无此邮箱的用户,请重新输入");
            return null;
        }

    }

    @Override
    public void updateUser(User user) {
        boolean flag = userDao.updateByObj(user);
        if(flag){
            JOptionPane.showMessageDialog(null,"更新成功");
        }else {
           JOptionPane.showMessageDialog(null,"更新失败");
        }
    }

    @Override
    public boolean insertUser(User user) {
        if (CommenUtil.inNull(user.getLoginName())){
            JOptionPane.showMessageDialog(null,"登录名为空");
            return false;
        }
        /*保证用户名唯一*/
        User user1 = new User();
        user1.setLoginName(user.getLoginName());
        List<User> userList = userDao.findObjByObj(user);
        if (userList.size()>0){
            JOptionPane.showMessageDialog(null,"登录名已存在");
            return false;
        }
        if (CommenUtil.inNull(user.getPassWord())){
            JOptionPane.showMessageDialog(null,"密码为空");
            return false;
        }
        if (CommenUtil.inNull(user.getName())){
            JOptionPane.showMessageDialog(null,"姓名为空");
            return false;
        }
        if (CommenUtil.inNull(user.getTel())){
            JOptionPane.showMessageDialog(null,"号码为空");
            return false;
        }
        if (CommenUtil.inNull(user.getAddress())){
            JOptionPane.showMessageDialog(null,"收货为空");
            return false;
        }
        boolean flag = userDao.insert(user);
        if (flag){
            JOptionPane.showMessageDialog(null,"注册成功");
            return true;
        }else {
            JOptionPane.showMessageDialog(null,"注册失败 id存在");
            return false;
        }

    }

    @Override
    public void delectUser(int id) {
        /*需要给出提示 是否删除 防止危险操作*/
        /*查找id是否存在*/
        int opt = JOptionPane.showConfirmDialog(null, "确认删除此用户吗?", "确认信息", JOptionPane.YES_NO_OPTION);
        if (opt == JOptionPane.NO_OPTION){
            return;
        }
        User user = new User();
        user.setId(id);
        User user1 = userDao.findObjByObj(user).get(0);
        if (user1!=null){
            /*id存在调用删除*/
            boolean flag = userDao.deleteObjById(id);
            if (flag){
               JOptionPane.showMessageDialog(null,"删除成功");
            }else {
                JOptionPane.showMessageDialog(null,"删除失败");
            }
        }else {
            System.out.println("没有此id的用户");
        }

    }

    @Override
    public List<User> findUserByUser(User user) {
        List<User> userList = userDao.findObjByObj(user);
        return userList;
    }

    @Override
    public List<User> findUserByPage(User user, int pageNum, int pageSize) {
        List<User> userlist = userDao.findObjByPage(user, pageNum, pageSize);
        return userlist;
    }

    @Override
    public String sendVarCode(String email) {

        Random random = new Random();
        /*生产四位随机验证码   并存储*/
        String verCode = String.valueOf(random.nextInt(9999));
        try {
            SendEmaiUtil.sendMail(verCode,email);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        return verCode;
    }
}
